package com.xiaomage.view.animation11

import android.app.Activity
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.GridLayout
import android.widget.GridView
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_grid_layout_animation.*

class GridLayoutAnimationActivity : AppCompatActivity() {
    private var data=ArrayList<String>()
    private lateinit var activity:Activity
    private lateinit var adapter:gridLayoutAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_grid_layout_animation)
        activity=this
        adapter=gridLayoutAdapter()
        data.addAll(getData())
        gridView.adapter=adapter
        bt_add.setOnClickListener{
            addData()
        }
    }
    fun addData(){
        data.addAll(getData())
        adapter.notifyDataSetChanged()
    }

    fun getData():ArrayList<String>{
        val data=ArrayList<String>()
        for(i in 0 until 100){
            data.add("测试"+i)
        }
        return data
    }
    private inner  class gridLayoutAdapter:BaseAdapter(){
        override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
            val tv=TextView(activity)
            tv.text=data[p0]
            tv.layoutParams=ViewGroup.LayoutParams(
                    GridLayout.LayoutParams.WRAP_CONTENT,
                    GridLayout.LayoutParams.WRAP_CONTENT
            )
            return tv
        }

        override fun getItem(p0: Int)=null

        override fun getItemId(p0: Int)=p0.toLong()

        override fun getCount()=data.size

    }
}
